package cn.iocoder.yudao.module.system.controller.admin.tenant;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.complaint.TenantComplaintPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.complaint.TenantComplaintRespVO;
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.complaint.TenantComplaintSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantComplaintDO;
import cn.iocoder.yudao.module.system.service.tenant.TenantComplaintService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 租户投诉")
@RestController
@RequestMapping("/system/tenant-complaint")
@Validated
public class TenantComplaintController {

    @Resource
    private TenantComplaintService tenantComplaintService;

    @PostMapping("/create")
    @Operation(summary = "创建租户投诉")
    //@PreAuthorize("@ss.hasPermission('system:tenant-complaint:create')")
    public CommonResult<Long> createTenantComplaint(@Valid @RequestBody TenantComplaintSaveReqVO createReqVO) {
        return success(tenantComplaintService.createTenantComplaint(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新租户投诉")
    //@PreAuthorize("@ss.hasPermission('system:tenant-complaint:update')")
    public CommonResult<Boolean> updateTenantComplaint(@Valid @RequestBody TenantComplaintSaveReqVO updateReqVO) {
        tenantComplaintService.updateTenantComplaint(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除租户投诉")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('system:tenant-complaint:delete')")
    public CommonResult<Boolean> deleteTenantComplaint(@RequestParam("id") Long id) {
        tenantComplaintService.deleteTenantComplaint(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得租户投诉")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('system:tenant-complaint:query')")
    public CommonResult<TenantComplaintRespVO> getTenantComplaint(@RequestParam("id") Long id) {
        TenantComplaintDO tenantComplaint = tenantComplaintService.getTenantComplaint(id);
        return success(BeanUtils.toBean(tenantComplaint, TenantComplaintRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得租户投诉分页")
    //@PreAuthorize("@ss.hasPermission('system:tenant-complaint:query')")
    public CommonResult<PageResult<TenantComplaintRespVO>> getTenantComplaintPage(@Valid TenantComplaintPageReqVO pageReqVO) {
        PageResult<TenantComplaintDO> pageResult = tenantComplaintService.getTenantComplaintPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TenantComplaintRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出租户投诉 Excel")
    //@PreAuthorize("@ss.hasPermission('system:tenant-complaint:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTenantComplaintExcel(@Valid TenantComplaintPageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TenantComplaintDO> list = tenantComplaintService.getTenantComplaintPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "租户投诉.xls", "数据", TenantComplaintRespVO.class, BeanUtils.toBean(list, TenantComplaintRespVO.class));
    }

}